<template>
    <div class="app-container">
        <el-card>
            <el-row>
                <el-col :span="1.5">
                    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
                        <el-form-item prop="warehouseName">
                            <el-input
                                v-model="queryParams.areaName"
                                placeholder="请输入仓库名称"
                                clearable
                                @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                    </el-form>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAddck"
                        v-hasPermi="['wms:warehouse:add']"
                    >新增仓库</el-button>
                </el-col>
            </el-row>
        </el-card>
        <el-divider />
        <el-card>
            <el-tabs :tab-position="tabPosition" @tab-click="selectQuery"
                     style="height: 560px;
                     margin-right:200px"
                     class="demo-tabs">
            <el-tab-pane v-for="it in warehouseList"
            :label="it.warehouseName"
            :key="it.id"
            :name="it.warehouseId">
                <div class="content-wrapper">
                    <div class="clearfix">
                        <div class="content-margin left"
                             style="color: rgb(64, 158, 255);
                             font-weight: bold; font-size: 15px;margin-top: 10px">
                            基本信息
                             <el-link type="primary" @click="handleUpdate">修改</el-link>   
                            <el-link type="danger" @click="handleDelete">删除</el-link>
                        </div>
                    </div>
                <div class="content-margin info-box" style="margin-top: 16px;">
                    <div class="el-row" style="margin-left: -10px; margin-right: -10px;">
                        <div class="el-col el-col-8" style="padding-left: 10px; padding-right: 10px;">
                            <div>
                                <div style="font-weight: bold;margin-right: 20px;width: 100px">
                                    仓库名称
                                </div>
                                <div style="margin-top: 10px;">
                                    {{ it.warehouseName }}
                                </div>
                            </div>
                        </div>
                        <div class="el-col el-col-8" style="padding-left: 10px; padding-right: 10px;">
                            <div>
                                <div style="font-weight: bold;">
                                    仓库编号
                                </div>
                                <div style="margin-top: 10px;">
                                    {{ it.warehouseId }}
                                </div>
                            </div>
                        </div>
                        <div class="el-col el-col-8" style="padding-left: 10px; padding-right: 10px;">
                            <div><div style="font-weight: bold;">
                                仓库备注
                            </div>
                                <div style="margin-top: 10px;">
                                    {{ it.remark }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </el-tab-pane>
        <el-tabs :tab-position="tabPosition1" style="height: 400px;margin-top: 15px" class="demo-tabs">
            <el-tab-pane label="库区信息">
                <el-row>
                    <el-form-item prop="areaName">
                        <el-input
                            v-model="queryParams.areaName"
                            placeholder="请输入库区的名称"
                            clearable
                            @keyup.enter="handleQuery"
                        />
                    </el-form-item>
                    <el-col :span="1.5">
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                            type="primary"
                            plain
                            icon="Plus"
                            @click="handleAdd"
                            v-hasPermi="['wms:area:add']"
                        >新增库区</el-button>
                    </el-col>
                </el-row>
                <el-col :span="20" :xs="24">
                    <el-table v-loading="loading" :data="areaList" @selection-change="handleSelectionChange">
                        <el-table-column label="编号" align="center" prop="areaNo" />
                        <el-table-column label="名称" align="center" prop="areaName" />
                        <el-table-column label="备注" align="center" prop="remark" />
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button link type="primary" icon="Edit" @click="handleUpdatekq(scope.row)" v-hasPermi="['wms:area:edit']">修改</el-button>
                                <el-button link type="primary" icon="Delete" @click="handleDeletekq(scope.row)" v-hasPermi="['wms:area:remove']">删除</el-button>
                            </template>
                        </el-table-column>

                    </el-table>
                </el-col>
            </el-tab-pane>
        </el-tabs>
    </el-tabs>

    </el-card>

        <!-- 添加或修改库区对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="areaRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="编号" prop="areaNo">
                    <el-input v-model="form.areaNo" placeholder="请输入编号" />
                </el-form-item>
                <el-form-item label="名称" prop="areaName">
                    <el-input v-model="form.areaName" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="所属仓库" prop="warehouseName">
                    <el-input v-model="form.warehouseName"/>
                </el-form-item>
                <el-form-item label="所属仓库ID" prop="warehouseId">
                    <el-input v-model="form.warehouseId"/>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>

        <!-- 添加或修改仓库对话框 -->
        <el-dialog :title="title2" v-model="open1" width="500px" append-to-body>
            <el-form ref="warehouseRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="编号" prop="warehouseId">
                    <el-input v-model="form.warehouseId" placeholder="请输入编号" />
                </el-form-item>
                <el-form-item label="名称" prop="warehouseName">
                    <el-input v-model="form.warehouseName" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitFormcq">确 定</el-button>
                    <el-button @click="cancel1">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script setup name="Warehouse">
import {
    listWarehouse,
    getWarehouse,
    delWarehouse,
    addWarehouse,
    updateWarehouse,
} from "@/api/wms/warehouse";
import { del,selectlist,listArea, getArea, delArea, addArea, updateArea } from "@/api/wms/area";
import { reactive } from "vue";


const { proxy } = getCurrentInstance();
const areaList = ref([]);
const warehouseList = ref([]);
const addPd=ref()
//库区对话框
const open = ref(false);
//仓库对话框
const open1 = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const title2 = ref("");
const tabPosition = ref("left")
const tabPosition1 = ref("top")
const row = ref([])
const kqID=ref("")
const ckName=ref("")
//仓库表单校验
const data = reactive({
    //仓库
    form:{},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    warehouseNo: null,
    warehouseName: null,
  },
  rules: {
    warehouseId: [
      { required: true, message: "仓库编号不能为空", trigger: "blur" }
    ],
    warehouseName: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
      areaNo: [
          { required: true, message: "编号不能为空", trigger: "blur" }
      ],
      areaName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
      ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询仓库列表 */
function getListcq() {
  loading.value = true;
  listWarehouse(queryParams.value).then(response => {
    warehouseList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

//查询库区列表
function getList() {
    loading.value = true;
    listArea(queryParams.value).then(response => {
        areaList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

//根据仓库id查询库区信息
function selectQuery(tab){
    selectlist(tab.props.name).then(response => {
        ckName.value=tab.props.label
        areaList.value = response.rows;
        kqID.value=tab.props.name
    })
    
}

// 取消按钮
function cancel1() {
  open1.value = false;
  reset();
}// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 仓库表单重置
function reset() {
  form.value = {
    id: null,
    warehouseId: null,
    warehouseName: null,
    areaNo: null,
    areaName: null,
    warehouseId: null,
    delFlag: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
    
  proxy.resetForm("warehouseRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getListcq();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增仓库按钮操作 */
function handleAddck() {
  reset();
  open1.value = true;
  title2.value = "添加仓库";
}

/** 新增库区按钮操作 */
function handleAdd() {
   reset();
   form.value.warehouseId=kqID.value
    open.value = true;
    title.value = "添加库区";
}


/** 修改仓库按钮操作 */
function handleUpdate() {
getWarehouse(kqID.value).then(response=>{
  title2.value="修改仓库"
  open1.value=true
  form.value=response.data
})

}

/** 新增库区按钮操作 */
function handleAddkq() {
    reset();
    open.value = true;
    title.value = "添加库区";
    
}

/** 修改库区按钮操作 */
function handleUpdatekq(row) {
    reset();
    const _id = row.areaNo || ids.value
    getArea(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改库区";
    });
}

/** 提交库区按钮 */
function submitForm() {
    proxy.$refs["areaRef"].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateArea(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                    getListcq();
                });
            } else {
                addArea(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}



/** 提交仓库按钮 */
function submitFormcq() {
  proxy.$refs["warehouseRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWarehouse(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open1.value = false;
          getListcq();
        });
      } else {
           listWarehouse().then(response=>{
            addPd.value="";
             response.rows.forEach(element => {
            if(element.warehouseId==form.value.warehouseId||element.warehouseName==form.value.warehouseName){
                addPd.value=false
             proxy.$modal.msgSuccess("该仓库编号已存在");  
                }
            })
        if(addPd.value!=false){
            addWarehouse(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open1.value = false;
          getListcq();
        });
            }
           })
           
       
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete() {
  proxy.$modal.confirm('是否确认删除"' + ckName.value + '"？').then(function() {
    selectlist(kqID.value).then(response=>{
      if(response.rows!=0){
      delArea(kqID.value)
      }
    })
    return delWarehouse(kqID.value);
  }).then(() => {
    getList();
    getListcq();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

function handleDeletekq(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除库区编号为"' + _ids + '"的数据项？').then(function() {
        return del(_ids);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/warehouse/export', {
    ...queryParams.value
  }, `warehouse_${new Date().getTime()}.xlsx`)
}

getListcq();
getList()
</script>

<style>
.demo-tabs > .el-tabs__content {
    /*padding: 32px;*/
    color: #6b778c;
    font-size: 12px;
    font-weight: 600;
}

.el-tabs--right .el-tabs__content,
.el-tabs--left .el-tabs__content {
    height: 100%;
}
</style>
